package com.yidingdui.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yidingdui.domain.entity.User;
import org.apache.ibatis.annotations.*;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Insert("INSERT INTO users (username, email, phone, password, created_at, updated_at) " +
            "VALUES (#{username}, #{email}, #{phone}, #{password}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertUser(User user);

    @Select("SELECT COUNT(*) FROM users WHERE username = #{username}")
    int countByUsername(String username);

    @Select("SELECT COUNT(*) FROM users WHERE email = #{email}")
    int countByEmail(String email);

    @Select("SELECT COUNT(*) FROM users WHERE phone = #{phone}")
    int countByPhone(String phone);

    @Select("SELECT id, username, email, phone, password, created_at AS createdAt, updated_at AS updatedAt FROM users WHERE username = #{username}")
    User getUserByUsername(String username);

    @Select("SELECT id, username, email, phone, password, created_at AS createdAt, updated_at AS updatedAt FROM users WHERE id = #{userId}")
    User getById(Long userId);

    @Update("UPDATE users SET password = #{password}, updated_at = #{updatedAt} WHERE id = #{id}")
    int updateUserPassword(User user);
}
